# coding=utf-8
# !/usr/bin/python3
# @Time:2021/3/3 9:46 上午
# @Author:wangyun
# @File:tasks.py

# 定义任务
from django.core.mail import send_mail
from celery_tasks.main import celery_app
from django.conf import settings
import logging

logger = logging.getLogger('django')


# bind:保证task对象会作为第一个参数自动传入
# name:队列名称
# retry_backoff ：异常自动重试的时间间隔，第n次
# max_retries :异常自动重试次数的上限

@celery_app.task(bind=True, name='send_verify_email', retry_backoff=3)
def send_verify_email(self, to_email, verify_url):
    """
    :param subject: 标题
    :param message: 消息
    :param from_email: 发件人
    :param recipient_list: 接受人列表
    :param fail_silently:
    :param auth_user:
    :param auth_password:
    :param connection:
    :param html_message: 富文本
    :return:
    """
    subject = 'nxh邮箱验证'
    html_message = '<p>尊敬的用户您好！</p>' \
                   '<p>感谢使用商城</p>' \
                   '<p>您的邮箱为：%s。请点击链接激活您的邮箱：</p>' \
                   '<p><a href="%s">%s</a></p>' % (to_email, verify_url, verify_url)
    try:

        send_mail(subject=subject, message='', from_email=settings.DEFAULT_FROM_EMAIL,
                           recipient_list=[to_email],
                           html_message=html_message)
    except Exception as e:
        logger.error(e)
        self.retry(exc=e, max_retries=3)

    # send_mail(subject, message, from_email, recipient_list,
    #           fail_silently=False, auth_user=None, auth_password=None,
    #           connection=None, html_message=None):

